package com.von.utils;

import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * Created by echisan on 2018/6/23
 */
@Component
public class TokenUtils {

    /**
     * 创建一个token
     *
     * @return
     */
    public static String createToken(String Prefix) {
        int i = (int) (Math.random() * 100);
        String token = "TK-" + Prefix + "-" + getRandomString(13).toUpperCase() + String.format("%02d", i);
        return token;
    }

    public static String getRandomString(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
